/**
 * 多级字典表控制器
 */
export default class MdictController extends think.BaseController {
  /**
     * 根据搜索条件查询字典列表
     */
  async queryDictBySeachAction() {
    const data = this.post('tree_name,status');
    const DictService = this.service('sys/MdictService');
    const list = await DictService.queryDictBySeach(data);
    if (list && list.error) {
      return this.errorJson(list.error);
    } else {
      return this.successJson(list);
    }
  }

  /**
     * 查询字典列表
     */
  async queryDictListAction() {
    const DictService = this.service('sys/MdictService');
    const list = await DictService.queryDictList();
    return this.successJson(list);
    if (list && list.error) {
      return this.errorJson(list.error);
    } else {
      return this.successJson(list);
    }
  }

  /**
     * 保存字典
     */
  async saveMDictAction() {
    const data = this.post('parent_id,tree_name,sort,description,remarks');
    const DictService = this.service('sys/MdictService');
    data.create_by = data.update_by = this.getUser().userId;
    const res = await DictService.saveMDict(data);
    if (res && res.error) {
      return this.errorJson(res.error);
    } else {
      return this.successJson(res);
    }
  }

  /**
     * 修改字典
     */
  async updateMDictAction() {
    const data = this.post('id,parent_id,tree_name,sort,description,remarks');
    data.update_by = this.getUser().userId;
    const DictService = this.service('sys/MdictService');
    const res = await DictService.updateMDict(data);
    if (res && res.error) {
      return this.errorJson(res.error);
    } else {
      return this.successJson(res);
    }
  }

  /**
     * 删除多级字典
     */
  async deleteMDictAction() {
    const id = this.post('id');
    const MDictService = this.service('sys/MdictService');
    const res = await MDictService.deleteMDict(id);
    if (res && res.error) {
      return this.errorJson(res.error);
    } else {
      return this.successJson(res);
    }
  }

  /**
     * 修改多级字典状态
     */
  async updateDictStatusAction() {
    const id = this.post('id');
    const status = this.post('status');
    const MDictService = this.service('sys/MdictService');
    const res = await MDictService.updateDictStatus(id, status);
    if (res && res.error) {
      return this.errorJson(res.error);
    } else {
      return this.successJson(res);
    }
  }
}
